package com.cloud.punish.api.web;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cloud.punish.api.bean.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.punish.api.service.RecPunishService;
import com.cloud.punish.common.consts.EnumNameConst;
import com.cloud.punish.common.context.HumanSession;
import com.cloud.punish.common.exception.BizException;
import com.cloud.punish.common.result.Quokka;
import com.cloud.punish.common.result.SelectItem;
import com.cloud.punish.common.utils.SessionHelper;
import com.cloud.punish.interfaces.FeignClientLawCase;
import com.cloud.punish.interfaces.FeignClientSystem;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("punish/api/rec")
public class RecPunishController {

	@Autowired
	private FeignClientSystem feignClientSystem;

	@Autowired
	private RecPunishService recPunishService;

	@Autowired
	private FeignClientLawCase feignClientLawCase;

	@GetMapping("dicts")
	public Quokka loadDicts(Long sysId) {
		Quokka qk = new Quokka(true);
		Map<String, Object> map = new HashMap<String, Object>();
		//判断登录人的单位是否为行政区划，如果是行政区划的则不需要进行领域过滤，否则进行领域过滤
		List<SelectItem> items = feignClientSystem.findSubjectDomain(SessionHelper.get().getUnitId());
		if (null == items || items.size() == 0) {
			throw new BizException(500, "所属部门执法领域未配置");
		}
		map.put("domains", items);
		Map<String, List<SelectItem>> dicts = feignClientSystem.loadDictItems(Arrays.asList(EnumNameConst.enumItems));
		map.putAll(dicts);
		qk.setData(map);
		return qk;
	}

	@PostMapping("saveupdate")
	public Quokka savaUpdate(@RequestBody RecPunishForm punish) {
		return new Quokka(true, recPunishService.saveUpdate(punish));
	}

	@GetMapping("load")
	public Quokka load(long recId) {
		return new Quokka(true, recPunishService.load(recId));
	}

	@PostMapping("searchlawcause")
	public Quokka searchLawCause(@RequestBody LawCauseSearchCriteria criteria) {
		PageInfo<LawCauseItem> pageInfo = feignClientLawCase.lawCausePagination(criteria);
		return new Quokka(true, pageInfo);
	}

	@PostMapping("search")
	public Quokka search(@RequestBody RecPunishSearchCriteria criteria) {
		HumanSession session = SessionHelper.get();
		if (session.getHumanId() > 0) {
			List<SelectItem> unitDomainItems = feignClientSystem.loadUnitDomains(SessionHelper.get().getUnitId());
			for (SelectItem item : unitDomainItems) {
				criteria.getDomains().add((String) item.getValue());
			}
			HumanDetail humanDetail = feignClientSystem.getHumanDetail(session.getHumanId());
			criteria.setRegionCode(humanDetail.getRegionCode());
			criteria.setUnitCode(humanDetail.getUnitCode());
		}
		return new Quokka(true, recPunishService.searchPunishPage(criteria));
	}

	@PostMapping("stat")
	public Quokka stat(@RequestBody RecPunishStatCriteria criteria) {
		HumanSession session = SessionHelper.get();
		if (session.getHumanId() > 0) {
			List<SelectItem> unitDomainItems = feignClientSystem.loadUnitDomains(SessionHelper.get().getUnitId());
			for (SelectItem item : unitDomainItems) {
				criteria.getDomains().add((String) item.getValue());
			}
			HumanDetail humanDetail = feignClientSystem.getHumanDetail(session.getHumanId());
			criteria.setRegionCode(humanDetail.getRegionCode());
		}
		return new Quokka(true, recPunishService.statPunishPage(criteria));
	}

	@GetMapping("searchlawyer")
	public Quokka searchLawyer(String query) {
		long unitId = SessionHelper.get().getUnitId();
		return new Quokka(true, feignClientSystem.searchLawyerByUnit(unitId, query));
	}

	@RequestMapping("cancel")
	public Quokka cancel(@RequestParam(required = true) Long recId) {
		recPunishService.cancel(recId);
		return new Quokka(true);
	}

	@RequestMapping("address")
	public Quokka loadAddressItems() {
		return new Quokka(true, feignClientSystem.loadAddressItems());
	}

	@RequestMapping("loadhuman")
	public Quokka loadHuman(Long unitId) {
		return new Quokka(true, feignClientSystem.loadHuman(unitId));
	}

	@RequestMapping("searchsuperviseitem")
	public Quokka searchSuperviseItem(@RequestBody RecPunishSuperviseItemCriteriaBean recPunishSuperviseItemCriteriaBean) {
		return new Quokka(true, feignClientSystem.searchSuperviseItem(recPunishSuperviseItemCriteriaBean));
	}
	
	@RequestMapping("searchlaw")
	public Quokka searchLaw(String title, int type){
		return new Quokka(true, feignClientLawCase.searchLaw(title, type));
	}
	
	@RequestMapping("searchlawitem")
	public Quokka searchLawItem(Long lawId, int type){
		return new Quokka(true, feignClientLawCase.searchLawItem(lawId, type));
	}

	/**
	 * 加载移交部门
	 * @param criteria
	 * @return
	 */
	@RequestMapping("listunitinfopage")
	public Quokka listUnitInfoPage(@RequestBody UnitCriteriaBean criteria) {
		return new Quokka(true, feignClientSystem.listUnitInfoPage(criteria));
	}

	/**
	 * 保存案卷移交信息
	 * @param recId
	 * @param unitId
	 * @return
	 */
	@RequestMapping("casetransfer")
	public Quokka caseTransfer(@RequestParam("recId") Long recId, @RequestParam("unitId") Long unitId){
		HumanDetail humanDetail = feignClientSystem.getHumanInfoByUnitId(unitId);
		return new Quokka(true, recPunishService.caseTransfer(recId, humanDetail));
	}
}
